﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Utils;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace LinkCard.ShopCP.UploadExcel
{
    public partial class file_list : System.Web.UI.Page
    {

        private int _totalRecords;
        private int nPageSize = 20;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null)
            {
                Response.Redirect("/login.aspx");
            }

            if (!Page.IsPostBack)
            {
                DataBindPaging(); 
            }

        }

        private void DataBindPaging()
        {
            try
            {
                Int64 nSupplierID = getSupplierID();
                string sFilter = "AND Supplier.SupplierID = "+nSupplierID+" AND FileUpload.Status = 0";
                int curpage = 0;

                if (Request.QueryString["page"] != null)
                {
                    curpage = Convert.ToInt32(Request.QueryString["page"]);
                }
                else
                {
                    curpage = 1;
                }

                DataTable dt = DataHelper.LoadPageInSQL("FileUploadList", curpage, nPageSize, ref _totalRecords, new SqlParameter("@Filter", sFilter));

                if (dt.Rows.Count > 0)
                {
                    int nPageCount;

                    if (_totalRecords % nPageSize == 0)
                    {
                        nPageCount = _totalRecords / nPageSize;
                    }
                    else
                    {
                        nPageCount = Convert.ToInt32(_totalRecords / nPageSize) + 1;
                    }

                    if (nPageCount < 2) divPager.Visible = false;

                    PagerShow.Text = hash1way.ShowPageNavigation(curpage, nPageCount);

                    msgList.DataSource = dt;

                    msgList.DataBind();
                    msgNoData.Visible = false;

                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void msgList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex < 0) return;
            DataRowView row = (DataRowView)e.Item.DataItem;

            var chkDelete = (HtmlInputCheckBox)e.Item.FindControl("chkDelete");
            chkDelete.Value = row["FileUploadID"].ToString();
        }

        protected Int64 getSupplierID()
        {
            try
            {

                if (Convert.ToInt32(Session["SHOPADMIN"]) == 1) // quanr ly cua hang
                {
                    return ConvertUtility.ToInt64(Session["USER"]);
                }
                else if (Convert.ToInt32(Session["SHOPADMIN"]) == 0)
                {
                    DataTable dt = DataHelper.SelectTable("SELECT SupplierID FROM [ShopStaff] WHERE Username ='" + Session["USER"].ToString() + "'", false);
                    if (dt.Rows.Count > 0)
                    {
                        return ConvertUtility.ToInt64(dt.Rows[0]["SupplierID"]);
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return 0;
                }
            }
            catch
            {
                return 0;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string sArray = "(0";

            foreach (RepeaterItem item in msgList.Items)
            {
                var chkDelete = (HtmlInputCheckBox)item.FindControl("chkDelete");
                if (chkDelete.Checked)
                {
                    sArray = sArray + "," + chkDelete.Value + "";
                }
            }
            sArray = sArray + ")";

            string sSQL = "delete [FileUpload] where FileUploadID in" + sArray;
            DataHelper.ExecuteNonQuery(sSQL, true);
            Response.Redirect("/UploadExcel/file_list.aspx");
        }

    }
}